package cn.amossun.starter.web.data.security.config;

import cn.amossun.starter.mybatis.data.security.rewrite.DecryptPreParameterRewriter;
import cn.amossun.starter.mybatis.data.security.rewrite.EncryptPreParameterRewriter;
import cn.amossun.starter.mybatis.data.security.rule.MybatisDecryptRule;
import cn.amossun.starter.mybatis.data.security.rule.MybatisEncryptRule;
import cn.amossun.starter.redis.cache.config.MultiRedisConfiguration;
import cn.amossun.starter.redis.cache.properties.MultiRedisProperties;
import cn.amossun.starter.web.data.security.IDataSecurityVersion;
import cn.amossun.starter.web.data.security.handler.DataSecurityConfigHandler;
import cn.amossun.starter.web.data.security.handler.DataSecurityVersionHandler;
import cn.amossun.starter.web.data.security.handler.EncryptDecryptDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @program: starter-parent
 * @description:
 * @author: Amos.Sun
 * @DateTime: 2020/03/20 19:14
 **/
@Configuration(proxyBeanMethods = false)
public class WebDataSecurityConfiguration {

    @ConditionalOnBean({EncryptPreParameterRewriter.class, DecryptPreParameterRewriter.class})
    public class WebEncryptDecryptManager {

        @Bean
        @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
        public EncryptDecryptDataHandler encryptDecryptDataHandler(MybatisDecryptRule mybatisDecryptRule, MybatisEncryptRule mybatisEncryptRule) {
            return new EncryptDecryptDataHandler(mybatisDecryptRule, mybatisEncryptRule);
        }

    }

    @ConditionalOnBean(MultiRedisConfiguration.class)
    public class WebConfigManager {

        @Bean
        @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
        public DataSecurityConfigHandler dataSecurityConfigHandler(@Autowired(required = false)
                                                                           @Qualifier("amosMultiRedisTemplate") RedisTemplate amosMultiRedisTemplate,
                                                                   IDataSecurityVersion dataSecurityVersion,
                                                                   MultiRedisProperties multiRedisProperties) {
            return new DataSecurityConfigHandler(amosMultiRedisTemplate, dataSecurityVersion, multiRedisProperties);
        }

        @Bean
        @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
        public DataSecurityVersionHandler dataSecurityVersionHandler(@Autowired(required = false) @Qualifier("amosMultiRedisTemplate") RedisTemplate amosMultiRedisTemplate){
            return new DataSecurityVersionHandler(amosMultiRedisTemplate);
        }


    }


}
